package com.iceberg.service.impl.database;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.iceberg.dao.database.DatabaseDAO;
import com.iceberg.model.database.Column;
import com.iceberg.model.database.Schema;
import com.iceberg.model.database.Table;
import com.iceberg.model.database.TableData;
import com.iceberg.model.database.View;
import com.iceberg.model.jqgrid.JqGridTable;
import com.iceberg.model.layout.TreeNode;
import com.iceberg.service.database.DatabaseService;

public class DatabaseServiceImpl implements DatabaseService {
  DatabaseDAO databaseDAO = null;

  public DatabaseDAO getDatabaseDAO() {
    return databaseDAO;
  }

  public void setDatabaseDAO(DatabaseDAO databaseDAO) {
    this.databaseDAO = databaseDAO;
  }

  @Override
  public List<TreeNode> buildNavigatorTree(String user) {
    //1. get database schema
    List<TreeNode> treeNodes = new ArrayList<TreeNode>();
    HashMap<String, String> attributes = new HashMap<String, String>();
    attributes.put("url", "/iceberg/databaseAction");

    TreeNode root = new TreeNode();
    root.setId("root");
    root.setText("root");
    treeNodes.add(root);
    buildSchemaTree(root, attributes);
    return treeNodes;
  }

  private void buildSchemaTree(TreeNode root, HashMap<String, String> attributes) {
    List<Schema> schemaList = databaseDAO.getSchemaList("");
    if (schemaList != null) {
      for (Schema schema : schemaList) {
        TreeNode schemaTreeNode = new TreeNode();
        schemaTreeNode.setText(schema.getName());
        schemaTreeNode.setId(schema.getId());
        root.addChild(schemaTreeNode);
        buildTableTree(schemaTreeNode, attributes);
        buildViewTree(schemaTreeNode, attributes);
      }
    }
  }

  private void buildViewTree(TreeNode schemaTreeNode, HashMap<String, String> attributes) {
    List<View> viewList = databaseDAO.getViewList(schemaTreeNode.getText(), schemaTreeNode.getText());
    if (viewList != null) {
      TreeNode tablesTreeNode = new TreeNode();
      tablesTreeNode.setText("views");
      tablesTreeNode.setId("views");
      schemaTreeNode.addChild(tablesTreeNode);

      for (View view : viewList) {
        //create table nodes and add url
        String tableName = view.getName();
        TreeNode tableTreeNode = new TreeNode();
        tableTreeNode.setText(tableName);
        tableTreeNode.setId(tableName);
        tableTreeNode.setAttributes(attributes);
        tablesTreeNode.addChild(tableTreeNode);
      }
    }
  }

  private void buildTableTree(TreeNode schemaTreeNode, HashMap<String, String> attributes) {
    List<Table> tableList = databaseDAO.getTableList(schemaTreeNode.getText(), schemaTreeNode.getText());
    if (tableList != null) {
      TreeNode tablesTreeNode = new TreeNode();
      tablesTreeNode.setText("tables");
      tablesTreeNode.setId("tables");
      schemaTreeNode.addChild(tablesTreeNode);

      for (Table table : tableList) {
        //create table nodes and add url
        String tableName = table.getName();
        TreeNode tableTreeNode = new TreeNode();
        tableTreeNode.setText(tableName);
        tableTreeNode.setId(tableName);
        tableTreeNode.setAttributes(attributes);
        tablesTreeNode.addChild(tableTreeNode);
        // buildColumnTree(schemaTreeNode,tableTreeNode,attributes);
      }
    }
  }

  private void buildColumnTree(TreeNode schemaTreeNode, TreeNode tableTreeNode, HashMap<String, String> attributes) {
    List<Column> columnList = databaseDAO.getColumnList(schemaTreeNode.getText(), schemaTreeNode.getText(), tableTreeNode.getText());
    if (columnList != null) {
      for (Column column : columnList) {
        //create table nodes and add url
        String columnName = column.getName();
        TreeNode columnTreeNode = new TreeNode();
        columnTreeNode.setText(columnName);
        columnTreeNode.setId(columnName);
        tableTreeNode.addChild(columnTreeNode);

        TreeNode columnTypeTreeNodeTreeNode = new TreeNode();
        columnTypeTreeNodeTreeNode.setText("Type:" + column.getColumnType());
        columnTypeTreeNodeTreeNode.setId("Type:" + column.getColumnType());
        columnTreeNode.addChild(columnTypeTreeNodeTreeNode);

        TreeNode columnSizeTreeNode = new TreeNode();
        columnSizeTreeNode.setText("Size:" + column.getColumnDisplaySize());
        columnSizeTreeNode.setId("Size:" + column.getColumnDisplaySize());
        columnSizeTreeNode.addChild(columnSizeTreeNode);
      }
    }
  }

  @Override
  public List<Schema> getSchemaList(String databaseName) {
    return this.databaseDAO.getSchemaList(databaseName);
  }

  @Override
  public List<Table> getTableList(String catalogName, String schemeName) {
    return this.databaseDAO.getTableList(catalogName, schemeName);
  }

  @Override
  public List<View> getViewList(String catalogName, String schemeName) {
    return this.databaseDAO.getViewList(catalogName, schemeName);
  }

  @Override
  public List<Column> getColumnList(String catalogName, String schemaName, String tableName) {
    return this.databaseDAO.getColumnList(catalogName, schemaName, tableName);
  }

  @Override
  public Boolean dataUpdate(String tableName, Map<String, Object> parameters) {
    return this.databaseDAO.dataUpdate(tableName, parameters);
  }

  @Override
  public Boolean dataCreate(String tableName, Map<String, Object> parameters) {
    return this.databaseDAO.dataCreate(tableName, parameters);
  }

  @Override
  public Boolean dataDelete(String tableName, Map<String, Object> parameters) {
    return this.databaseDAO.dataDelete(tableName, parameters);
  }

  @Override
  public Table getTable(String catalogName, String schemeName, String tableName) {
    return this.databaseDAO.getTable(catalogName, schemeName, tableName);
  }

  @Override
  public List<Column> getPrimaryKeyColumnList(String catalogName, String schemaName, String tableName) {
    return this.databaseDAO.getPrimaryKeyColumnList(catalogName, schemaName, tableName);
  }

  @Override
  public List<Column> getForeignKeyColumnList(String catalogName, String schemaName, String tableName) {
    //TODO Auto-generated method stub
    throw new IllegalStateException("liyang didn't do his work!");
  }

  @Override
  public List<Column> getReferencedForeignKeyColumnList(String catalogName, String schemaName, String tableName) {
    //TODO Auto-generated method stub
    throw new IllegalStateException("liyang didn't do his work!");
  }

  @Override
  public JqGridTable getTableData(String tableName, JqGridTable jqGridTable) {
    return this.databaseDAO.getTableData(tableName, jqGridTable);
  }

}
